//package com.sharding.shardingdemo.config;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.google.common.collect.Lists;
//import com.sharding.shardingdemo.handler.ApacheShardingTable;
//import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
//import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
//import org.apache.shardingsphere.api.config.sharding.strategy.StandardShardingStrategyConfiguration;
//import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
//import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.*;
//
//public class ApacheShardingConfig {
//
//    /**
//     * sharding
//     * @author 孙灵达
//     */
//    @Bean
//    public DataSource shardingDataSource() throws SQLException, ReflectiveOperationException {
//        ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
//        shardingRuleConfiguration.getTableRuleConfigs().add(getUserRuleConfig());
//       // shardingRuleConfiguration.getTableRuleConfigs().add(getUserInfoRuleConfig());
//       // shardingRuleConfiguration.getBindingTableGroups().add("user, user_info");
//       // shardingRuleConfiguration.getBroadcastTables().add("dict");
//      //  shardingRuleConfiguration.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("phone", new ApacheShardingDatabase()));
//        shardingRuleConfiguration.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("phone", new ApacheShardingTable()));
//        //shardingRuleConfiguration.setMasterSlaveRuleConfigs(getMasterSlaveConfig());
//
//        Properties properties = new Properties();
//        properties.setProperty(ShardingPropertiesConstant.SQL_SHOW.getKey(), "true");
//        return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfiguration, properties);
//    }
//
//
//    @Bean
//    public SqlSessionFactoryBean sqlSessionFactoryBean() throws SQLException, ReflectiveOperationException {
//        DataSource dataSource = shardingDataSource();
//        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
//        sessionFactoryBean.setDataSource(dataSource);
//        return sessionFactoryBean;
//    }
//
//    @Bean
//    public DataSourceTransactionManager transactionManager() throws SQLException, ReflectiveOperationException {
//        DataSourceTransactionManager manager = new DataSourceTransactionManager();
//        manager.setDataSource(shardingDataSource());
//        return manager;
//    }
//
////    /**
////     * user_info表的分片规则
////     * @author 孙灵达
////     */
////    private TableRuleConfiguration getUserInfoRuleConfig() {
////        TableRuleConfiguration userInfoRuleConfig = new TableRuleConfiguration("user_info", "ds${0..1}.user_info_${0..2}");
////        KeyGeneratorConfiguration keyGeneratorConfiguration = new KeyGeneratorConfiguration("SNOWFLAKE", "id");
////        userInfoRuleConfig.setKeyGeneratorConfig(keyGeneratorConfiguration);
////        return userInfoRuleConfig;
////    }
//
//    /**
//     * user表的分片规则
//     * @author 孙灵达
//     */
//    private TableRuleConfiguration getUserRuleConfig() {
//        TableRuleConfiguration userRuleConfig = new TableRuleConfiguration("user_msg", "ds0.user_msg_${0..1}");
//        KeyGeneratorConfiguration keyGeneratorConfiguration = new KeyGeneratorConfiguration("SNOWFLAKE", "status");
//        userRuleConfig.setKeyGeneratorConfig(keyGeneratorConfiguration);
//        return userRuleConfig;
//    }
//
////    /**
////     * 获取主从数据源配置
////     * @author 孙灵达
////     */
////    private List<MasterSlaveRuleConfiguration> getMasterSlaveConfig() {
////        MasterSlaveRuleConfiguration msConfig1 =
////                new MasterSlaveRuleConfiguration("ds0", "ds_master1", Arrays.asList("ds_master1_slave1", "ds_master1_slave2"));
////        MasterSlaveRuleConfiguration msConfig2 =
////                new MasterSlaveRuleConfiguration("ds1", "ds_master2", Arrays.asList("ds_master2_slave1", "ds_master2_slave2"));
////        return Lists.newArrayList(msConfig1, msConfig2);
////    }
//    /**
//     * 数据源配置
//     * @author 孙灵达
//     */
//    private Map<String, DataSource> createDataSourceMap() throws ReflectiveOperationException {
//        final Map<String, DataSource> dbMap = new HashMap<>();
//        dbMap.put("ds0", getDataSource());
//        return dbMap;
//    }
//
//    /**
//     * 获取数据源
//     * @author 孙灵达
//     */
//    private DataSource getDataSource() {
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setDbType("com.alibaba.druid.pool.DruidDataSource");
//        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/sharding?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT");
//        dataSource.setUsername("root");
//        dataSource.setPassword("123456");
//        return dataSource;
//    }
//}
